
<template>
  <div>
    <van-nav-bar
    title="账号登录"
    left-arrow
    @click-left="$router.back()"
    />
   <van-form @submit="submit">
  <van-field
    v-model="username"
    name="username"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import {setKey} from '@/utils/storgs'
export default {
  name:'app',
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async submit(values){
      try {
      const res = await this.$http.post('/user/login',values)
      this.$toast('登陆成功')
      const token = res.data.body.token
      setKey('haoke',token)
      this.$router.push(`/layou/homepage`)
      }catch(e){
        console.log(e);
        this.$toast('登陆失败')
      }
    }
  },
};
</script>

<style lang="less" scoped>
</style>